/*
 * @Author: your name
 * @Date: 2020-02-21 17:36:31
 * @LastEditors: komons
 * @LastEditTime: 2020-03-11 17:51:07
 * @Description: 登录接口
 * @FilePath: \form-builderc:\Komons\学习\visualization\src\utils\api\userApi.js
 */
import axios from "axios";
import config from "../../config";
import { getToLocalstorage, getToSessionStorage, saveToSessionStorage } from "../storageUtils";

function initAxios() {
  let token = getToLocalstorage("token");
  const instance = axios.create({
    timeout: 1000,
    headers: { "x-jwt-token": token },
  });
  return instance;
}
// 登录接口
export function login(us, ps) {
  return axios.post(`${config.api}/user/login`, {
    us,
    ps,
  });
}

// 退出登录
export function logout() {
  return axios.get(`${config.api}/user/logout`);
}

// 判断是否登录
export function isLogin() {
  let token = getToLocalstorage("token");
  return new Promise((resolve, reject) => {
    axios
      .post(`${config.api}/user/islogin`, {
        token,
      })
      .then((res) => {
        resolve(res.data);
      })
      .catch((err) => {
        reject(err);
      });
  });
}

// 注册
export function register(data) {
  const { username, password, emailCode, email } = data;
  return new Promise((resolve, reject) => {
    axios
      .post(`${config.api}/user/reg`, {
        us: username,
        ps: password,
        code: emailCode,
        email,
      })
      .then((res) => {
        resolve(res.data);
      })
      .catch((err) => {
        reject(err);
      });
  });
}

// 获取邮箱验证码
export function getCode(mail) {
  return new Promise((resolve, reject) => {
    axios
      .post(`${config.api}/user/getMailCode`, {
        mail,
      })
      .then((res) => {
        resolve(res.data);
      })
      .catch((err) => {
        reject(err);
      });
  });
}

export function getUserDetail() {
  return new Promise((resolve, reject) => {
    initAxios()
      .get(`${config.api}/user/detail`)
      .then((res) => {
        resolve(res.data);
      })
      .catch((err) => {
        reject(err);
      });
  });
}

// 更新用户信息
export function updateUserDetail(data) {
  const { sex, avatar, name } = data;
  return new Promise((resolve, reject) => {
    initAxios()
      .post(`${config.api}/user/update`, {
        sex: sex,
        avatar: avatar,
        name,
      })
      .then((res) => {
        resolve(res.data);
      })
      .catch((err) => {
        reject(err);
      });
  });
}

// 获取头像列表
export function getAvatarList() {
  return new Promise((resolve, reject) => {
    initAxios()
      .get(`${config.api}/user/avatar`)
      .then((res) => {
        resolve(res.data);
      })
      .catch((err) => {
        reject(err);
      });
  });
}

// 获取背景
export function getBackground() {
  return new Promise((resolve, reject) => {
    let url = getToSessionStorage("bg_url");
    if (url) {
      resolve(url);
    } else {
      axios
        .get(
          "https://jsonp.afeld.me/?callback=&url=https%3A%2F%2Fcn.bing.com%2FHPImageArchive.aspx%3Fformat%3Djs%26cc%3Djp%26idx%3D0%26n%3D1"
        )
        .then((data) => {
          let url = "https://cn.bing.com/" + data.data.images[0].url;
          saveToSessionStorage("bg_url", url);
          resolve(url);
        })
        .catch((err) => {
          reject(err);
        });
    }
  });
}

// 获取头像列表
export function uploadAvatar(formData) {
  return new Promise((resolve, reject) => {
    initAxios()
      .post(`${config.api}/upload/avatar`, formData, {
        "Content-Type": "multipart/form-data",
      })
      .then((res) => {
        resolve(res.data);
      })
      .catch((err) => {
        reject(err);
      });
  });
}
